//role 对象
var Role = {
	id:"roleTable",		//表格id
	seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    //初始化表格的列
    initColumn:function () {
        var columns = [
            {field: 'selectItem', radio: true},
            {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
            {title: '序号', field: '', align: 'center', valign: 'middle', sortable: false,width:'50px',
            	formatter:function(value,row,index){
            		return index+1;
            	}
            },
            {title: '角色名称', field: 'roleName', align: 'center', valign: 'middle', sortable: true},
            {title: '角色代码', field: 'roleCode', align: 'center', valign: 'middle', sortable: true},
            {title: '是否启用', field: 'isEnable', align: 'center', valign: 'middle', sortable: true,
            	formatter:function(value,row,index){
            		 if(value==1){
                         return "已启用";
                     }
                     return '<span style="color:red">未启用</span>';
            	}
            }];
        return columns;
    },
    //检查是否选中
    check:function(){
        var selected = $('#' + this.id).bootstrapTable('getSelections');
        if (selected.length == 0) {
            Base.info("请先选中表格中的某一记录！");
            return false;
        } else {
            Role.seItem = selected[0];
            return true;
        }
    },
    //添加窗口
    addWindow:function () {
        var index = layer.open({
            type: 2,
            title: '添加角色信息',
            area: ['830px', '450px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Base.adminPath + '/sysRole/add'
        });
        this.layerIndex = index;
    },
    //修改窗口
    editWindow:function () {
        if (this.check()) {
            var index = layer.open({
                type: 2,
                title: '修改角色信息',
                area: ['800px', '450px'], //宽高
                fix: false, //不固定
                maxmin: true,
                content: Base.adminPath + '/sysRole/edit/' + this.seItem.id
            });
            this.layerIndex = index;
        }
    },
    //权限窗口
    menuWindow:function () {
    	if (this.check()) {
            var index = layer.open({
                type: 2,
                title: '分配权限',
                area: ['300px', '450px'], //宽高
                fix: false, //不固定
                maxmin: true,
                content: Base.adminPath + '/sysRole/menu/' + this.seItem.id
            });
            this.layerIndex = index;
        }
    },
    //删除
    doDelete:function () {
        if (this.check()) {
            var operation = function(){
                var ajax = new $ajax(Base.adminPath + "/sysRole/delete", function (data) {
                	if(data.status){
        				Base.success(data.data);
        				Role.table.refresh(); // 刷新列表
        			}else{
        				Base.error(data.message);
        			}
                }, function (data) {
                    Base.error("删除失败!");
                });
                ajax.set("id", Role.seItem.id);
                ajax.start();
            };

            Base.confirm("是否刪除该角色?", operation);
        }
    },
    doSearch:function () {
        var queryData = {};

        queryData['roleName'] = $("#roleName").val();

        Role.table.refresh({query: queryData});
    },
    resetSearch:function(){
    	$("#roleName").val("");

        Role.doSearch();
    }
}


$(function () {
	layui.use(['layer'], function(){
	  var layer = layui.layer;
	  
	});
    var defaultColunms = Role.initColumn();
    var table = new BSTable(Role.id, Base.adminPath + "/sysRole/list", defaultColunms);
    table.setPaginationType('server');
    table.init();
    Role.table = table;
});